﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

[assembly:CLSCompliant(true)]
namespace Netfraction
{
    [Flags()]
    public enum IdentityProperties : int
    {
        None,
        SessionId,
        ClientId,
        PrivateId,
        IPv4Address,
        IPv6Address,
        UDPv4Port,
        UDPv6Port,
        ShareSize,
        SharedFilesCount,
        ClientVersionIdentifier,
        MaximumUploadSpeed,
        MaximumDownloadSpeed,
        MaximumUploadSlots,
        AutomaticSlotAllocatorSpeedLimit,
        MinimumUploadConnectionsForAutoSlotAlloc,
        EmailAddress,
        NickOrHubName,
        Description,
        IsNormalUser,
        IsRegisteredUser,
        IsOperator,
        Token,
        ClientType,
        AwayStatus,
        SupportedFeatures,
        RefererUrl
    }

    public class IdentityUpdateEventArgs : EventArgs
    {
        public IdentityUpdateEventArgs(IdentityProperties updatedProperties)
        {
            this.UpdatedProperties = updatedProperties;
        }

        public IdentityProperties UpdatedProperties { get; set; }
    }

    public enum IdentityAwayStatus : int
    {
        Online = 0,
        Away = 1,
        ExtendedAway = 2
    }

    [Flags]
    public enum IdentityNodeTypes : int
    {
        NormalUser = 0,
        Bot = 1,
        RegisteredUser = 2,
        Operator = 4,
        SuperUser = 8,
        HubOwner = 16,
        Hub = 32
    }

    public class Identity
    {
        public Identity()
        {
            this.ClientExtendedFeatures = new List<Netfraction.Network.Protocol.ProtocolFeature>();
            this.IPv4Address = IPAddress.None;
        }

        public bool IsOperator
        {
            get
            {
                return (this.NodeType & IdentityNodeTypes.Operator) == IdentityNodeTypes.Operator;
            }
        }

        public string ClientId { get; set; }
        public string PrivateId { get; set; }
        public string Nickname { get; set; }
        public string Password { get; set; }
        public string Description { get; set; }
        public string Email { get; set; }
        public IdentityNodeTypes NodeType { get; set; }
        public int UploadSlots { get; set; }
        public Int64 MaximumUploadSpeed { get; set; }
        public Int64 MaximumDownloadSpeed { get; set; }
        public Int64 ShareSize { get; set; }
        public int SharedFilesCount { get; set; }
        public int HubNormalCount { get; set; }
        public int HubRegisteredCount { get; set; }
        public int HubOperatorCount { get; set; }
        public string ClientVersion { get; set; }
        public IdentityAwayStatus AwayStatus { get; set; }
        public int Udp4Port { get; set; }
        public List<Network.Protocol.ProtocolFeature> ClientExtendedFeatures { get; set; }
        public IPAddress IPv4Address { get; set; }
        public string Token { get; set; }
    }

    public class IdentitySort : System.Collections.IComparer//<Identity>
    {
        #region IComparer<Identity> Members

        public int Compare(object x, object y)
        {
            if (x == null || y == null)
            {
                //throw new ArgumentNullException("Identity Compare");
                return 0;
            }
            Identity xIdent = (x as Identity);
            Identity yIdent = (y as Identity);
            if (xIdent.IsOperator ^ yIdent.IsOperator)
            {
                if (xIdent.IsOperator)
                    return -1;
                else
                    return 1;
            }

            return string.Compare(xIdent.Nickname, yIdent.Nickname, StringComparison.OrdinalIgnoreCase);
        }

        #endregion
    }
}
